<template>
  <div v-show="userId" :class="{ 'userbar-container': 1, 'show': show }">
    <div class="userbar">
      <span class="jiantou"></span>
      <div class="block">
        <!-- 创建的歌单 -->
        <div class="create-playlists">
          <div class="title" @click.stop="showCearte = !showCearte">
            <p style="font-weight: 700;font-size:16px;color: #ee8f23;">创建的歌单<i
                :class="{ 'el-icon-caret-right': !showCearte, 'el-icon-caret-bottom': showCearte }"></i></p>
            <span style="font-weight: 700; font-size:16px" class="el-icon-plus" @click.stop="addNewPlaylists"></span>
          </div>
          <div class="play-items" v-show="showCearte">
            <p :class="{ 'my-love': 1, 'active': select }" @click="myLove"><i class="iconfont icon-aixin"></i>我喜欢的音乐</p>
            <p v-for="(item, i) in createPlay.slice(1)" :key="item.id"
              :class="{ 'my-playlists': 1, 'active': i == cIndex }" @click.self="createCurrentPlay(item.id, i)">
              <i class="iconfont icon-songlist"></i>
              {{ item.name }}
              <span class="el-icon-more" @click.stop="checkMoreInfo(item)"></span>
              <span class="edit-box" v-show="item.id == isShowEdit">
                <i class="el-icon-video-play" style="width:25%;color:rgb(60, 133, 228)"
                  @click.stop="playThisPlayList(item.id)"> 播放</i>
                <i class="el-icon-edit" style="width:25%;color:rgb(243, 142, 10)" @click.stop="editPlayList(item)">
                  编辑</i>
                <el-popconfirm style="width:40%;height:100%" title="确定删除此歌单吗？" confirm-button-type="danger"
                  @confirm="deletePlaylist(item.id)">
                  <template v-slot:reference>
                    <i class="el-icon-delete" style="width:100%;height:100%;color:rgb(240, 83, 44)">删除歌单</i>
                  </template>
                </el-popconfirm>
                <i class="el-icon-close" style="width:10%" @click.stop="isShowEdit = ''"></i>
              </span>
            </p>
          </div>
        </div>
        <!-- 收藏的歌单 -->
        <div class="sub-playlists">
          <div class="title" @click="showSub = !showSub">
            <p style="font-weight: 700;font-size:16px;color: #ee8f23;">收藏的歌单<i
                :class="{ 'el-icon-caret-right': !showSub, 'el-icon-caret-bottom': showSub }"></i></p>
          </div>
          <div class="play-items" v-show="showSub">
            <p v-for="(item, i) in subPlay" :key="item.id" :class="{ 'my-playlists': 1, 'active': i == index }"
              @click.self="currentPlay(item.id, i)">
              <i class="iconfont icon-songlist"></i>
              {{ item.name }}
              <span class="el-icon-more" @click.stop="checkMoreInfo(item)"></span>
              <span class="edit-box" v-show="item.id == isShowEdit">
                <i class="el-icon-video-play" style="width:45%;color:rgb(60, 133, 228)"
                  @click.stop="playThisPlayList(item.id)"> 播放</i>
                <el-popconfirm style="width:45%;height:100%" title="确定删除此歌单吗？" confirm-button-type="danger"
                  @confirm="deletePlaylist(item.id)">
                  <template v-slot:reference>
                    <i class="el-icon-delete" style="width:100%;height:100%;color:rgb(240, 83, 44)">删除歌单</i>
                  </template>
                </el-popconfirm>
                <i class="el-icon-close" style="width:10%" @click.stop="isShowEdit = ''"></i>
              </span>
            </p>
          </div>
        </div>
      </div>
    </div>

    <!-- 新建歌单窗口 -->
    <div :class="{ 'newbuild': 1, 'change': isAdd }">
      <h1>新建歌单</h1>
      <input type="text" placeholder="请输入新建歌单标题" v-model.trim="playName">
      <span @click="addPlaylists">创建</span>
      <i class="el-icon-close" @click="isAdd = false"></i>
    </div>

    <!-- 编辑歌单信息窗口 -->
    <div :class="{ 'edit-playlist': 1, 'change': isEdit }">
      <h1>编辑歌单信息</h1>
      <div class="playName-box"><input v-model.trim="updateName" class="playName" type="text"
          :placeholder="placeholder">
      </div>
      <textarea v-model.trim="playDesc" placeholder="请输入歌单简介："></textarea>
      <el-upload action="#" list-type="picture-card" :limit="1" :on-change="uploadFile" :auto-upload="false">
        <i class="el-icon-plus"></i>
      </el-upload>
      <el-button-group>
        <el-button type="warning" size="medium" @click.stop="updatePlayListInfo">确定</el-button>
        <el-button size="medium" @click.stop="handleReset">取消</el-button>
      </el-button-group>
    </div>
  </div>
</template>

<script>
import axios from "@/request/request";
import { mapGetters } from 'vuex'
export default {
  props: {
    show: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      showCearte: false,
      showSub: false,
      createPlay: [],
      subPlay: [],
      index: -1,
      cIndex: -1,
      select: false,
      isAdd: false,
      playName: '',
      isShowEdit: '',
      isEdit: false,
      placeholder: '',
      imageUrl: '',
      playlistId: '',
      file: null,
      updateName: '',
      playDesc: ''
    }
  },
  methods: {
    async getUserPlayList(isLoading = false) {
      const timestamp = +new Date()
      const { data: res } = await axios.get(`/user/playlist?uid=${this.userId}&timestamp=${timestamp}`, isLoading)
      if (res.code == 200) {
        this.createPlay = res.playlist.filter(item => item.subscribed == false)
        this.subPlay = res.playlist.filter(item => item.subscribed)
      }
    },
    currentPlay(id, i) {
      this.$router.push('/playlists/' + id)
      this.select = false
      this.cIndex = -1
      this.index = i
    },
    createCurrentPlay(id, i) {
      this.$router.push('/playlists/' + id)
      this.select = false
      this.index = -1
      this.cIndex = i
    },
    myLove() {
      this.index = -1
      this.cIndex = -1
      this.select = true
      this.$router.push('/playlists/' + this.createPlay[0].id)
    },
    addNewPlaylists() {
      this.isAdd = true
    },
    async addPlaylists() {
      if (this.playName == '') return this.$message.warning('请输入歌单名')
      const timestamp = +new Date()
      const { data: res } = await axios.post(`/playlist/create?name=${this.playName}&timestamp=${timestamp}`)
      console.log(res);
      if (res.code == 200) {
        this.$message.success('新建歌单成功')
        this.getUserPlayList()
        this.isAdd = false
        this.playName = ''
      }
    },
    checkMoreInfo(item) {
      this.isShowEdit = item.id
    },
    async deletePlaylist(id) {
      const timestamp = +new Date()
      const { data: res } = await axios.post(`/playlist/delete?id=${id}&timestamp=${timestamp}`)
      if (res.code == 200) {
        this.$message.success('删除歌单成功')
        this.getUserPlayList()
      }
    },
    playThisPlayList(id) {
      this.$store.dispatch('audio/getPlayListSongs', id)
    },
    editPlayList(item) {
      this.isEdit = true
      this.placeholder = item.name
      this.playlistId = item.id
    },
    async uploadFile(file) {
      this.file = file.raw
      console.log(this.file);
    },
    async updatePlayImage() {
      const formData = new FormData()
      formData.append('imgFile', this.file)
      const { data: res } = await axios.request({
        url: `/playlist/cover/update?id=${this.playlistId}`,
        method: 'post',
        data: formData,
        headers: {
          'Content-Type': 'multipart/form-data',
        }
      })
      return res
    },
    async updatePlayListInfo() {
      if (!this.updateName) return this.$message.error('请输入歌单名')
      const { data: res } = await axios.post(`/playlist/update?id=${this.playlistId}&name=${this.updateName}&desc=${this.playDesc}&tags=''`)
      console.log(res);
      const res2 = await this.updatePlayImage()
      if (res.code == 200 && (res2.code == 200 || !this.file)) {
        this.$message.success('更新歌单信息成功')
        this.getUserPlayList()
        this.isEdit = false
        this.isShowEdit = ''
      } else {
        this.$message.error('更新歌单信息失败')
      }
    },
    handleReset() {
      this.isEdit = false
      this.playDesc = ''
      this.updateName = ''
      this.file = null
    },
  },
  computed: {
    ...mapGetters('user', ['userId'])
  }
}
</script>

<style lang="less" scoped>
@tcolor: hotpink;

.userbar-container {
  position: absolute;
  top: 30px;
  left: 50%;
  transform: translateX(-50%);
  width: 270px;
  height: 0;
  box-sizing: border-box;
  transition: all 0.5s;
  overflow: hidden;
  cursor: default;

  .userbar {
    position: absolute;
    top: 40px;
    width: 100%;
    height: (120vh - 40px);
    border-radius: 5px;
  }

  .jiantou {
    z-index: 9999;
    position: absolute;
    left: 50%;
    top: -38px;
    transform: translateX(-50%);
    width: 0;
    height: 0;
    border: 20px solid transparent;
    border-left: 13px solid transparent;
    border-right: 13px solid transparent;
    border-bottom-color: rgb(109, 45, 236);
  }

  .block {
    width: 100%;
    height: 100%;
    overflow: auto;
    border-radius: 5px;
    // padding: 10px;
    backdrop-filter: blur(50px);
    border: 2px solid;
    // border-color: rgb(109, 45, 236);
    // background-image: linear-gradient();
    box-sizing: border-box;

    &::-webkit-scrollbar {
      display: none;
    }
  }

  &.show {
    width: 270px;
    height: 120vh;
    padding: 10px 0;
  }

  .create-playlists {
    box-sizing: border-box;
    width: 100%;
    padding: 10px 0;

    .title {
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 100%;
      padding: 0 10px;


      p {
        margin: 0;
        font-size: 14px;
        color: @tcolor;
      }
    }

    .play-items {
      width: 100%;

      p {
        box-sizing: border-box;
        width: 100%;
        margin: 0;
        text-align: start;
        font-size: 14px;
        padding: 10px;
        padding-right: 20px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;

        &.active {
          background-color: rgba(0, 0, 0, 0.5);
        }
      }

      .my-love {
        display: flex;
        align-items: center;
        margin-top: 10px;
        color: @tcolor;

        &:hover {
          background-color: rgba(255, 255, 255, 0.5);
        }

        &.active {
          background-color: rgba(0, 0, 0, 0.5);
        }

        .icon-aixin {
          color: rgb(207, 73, 73);
          font-size: 18px;
          margin-right: 3px;
        }
      }

      .my-playlists {
        position: relative;
        color: @tcolor;

        &:hover {
          background-color: rgba(255, 255, 255, 0.5);
        }

        .icon-songlist {
          // font-weight: 600;
          font-size: 18px;
          margin-right: 3px;
        }


      }
    }
  }

  .el-icon-more {
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    top: 50%;
    right: 0;
    transform: translateY(-50%);
    width: 30px;
    height: 100%;

    &:hover {
      background-color: rgb(255, 255, 255);
    }
  }

  .edit-box {
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    background-color: rgba(255, 255, 255, 0.8);
    text-align: center;

    i {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100%;

      &:hover {
        background-color: rgb(194, 194, 194);
      }
    }
  }

  .sub-playlists {
    box-sizing: border-box;
    width: 100%;
    padding: 10px 0;
    color: @tcolor;

    .title {
      box-sizing: border-box;
      display: flex;
      justify-content: space-between;
      align-items: center;
      width: 100%;
      padding: 0 10px;

      p {
        margin: 0;
        font-size: 14px;
        color: @tcolor;
      }
    }

    .play-items {
      width: 100%;

      p {
        box-sizing: border-box;
        width: 100%;
        margin: 0;
        text-align: start;
        font-size: 14px;
        padding: 10px;
        padding-right: 20px;
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;

        &.active {
          background-color: rgb(237, 237, 237);
        }
      }

      .my-playlists {
        position: relative;

        &:hover {
          background-color: rgba(255, 255, 255, 0.5);
        }

        .icon-songlist {
          // font-weight: 600;
          font-size: 18px;
          margin-right: 3px;
        }
      }
    }
  }

  .newbuild {
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
    align-items: center;
    position: absolute;
    top: 40px;
    left: 50%;
    transform: translateX(-50%) scale(0);
    width: 100%;
    height: 250px;
    border-radius: 5px;
    padding: 40px 0;
    background-color: rgba(117, 48, 195, 0.8);
    transition: all 0.5s;

    &.change {
      transform: translateX(-50%) scale(1);
    }

    h1 {
      margin: 0;
      font-size: 18px;
      color: rgb(255, 255, 255);
      font-weight: 400;
    }

    input {
      box-sizing: border-box;
      border: none;
      outline: none;
      width: 80%;
      height: 30px;
      border-radius: 15px;
      font-size: 15px;
      text-indent: 1em;

      &::placeholder {
        font-size: 14px;
        color: rgb(188, 188, 188);
      }
    }

    span {
      width: 25%;
      height: 20px;
      padding: 2px 15px;
      font-size: 14px;
      line-height: 20px;
      text-align: center;
      border-radius: 12px;
      color: rgb(255, 255, 255);
      background-color: rgb(226, 52, 159);
      transition: all 0.3s;

      &:hover {
        background-color: rgb(255, 0, 157);
      }
    }

    i {
      position: absolute;
      left: 50%;
      bottom: 20px;
      transform: translateX(-50%);
      font-size: 20px;
      color: rgb(255, 255, 255);
    }
  }

  .edit-playlist {
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    // justify-content: space-around;
    align-items: center;
    position: absolute;
    top: 40px;
    left: 50%;
    transform: translateX(-50%) scale(0);
    width: 100%;
    height: (120vh - 40px);
    border-radius: 5px;
    padding: 40px 0;
    background-color: rgba(117, 48, 195, 0.8);
    transition: all 0.5s;

    &.change {
      transform: translateX(-50%) scale(1);
    }

    h1 {
      margin: 0;
      font-size: 16px;
      color: rgb(255, 255, 255);
      font-weight: 400;
      margin-bottom: 20px;
    }

    .playName-box {
      width: 100%;
      margin-bottom: 20px;
    }

    .playName {
      display: inline-block;
      box-sizing: border-box;
      border: none;
      outline: none;
      width: 90%;
      height: 30px;
      // border-radius: 15px;
      font-size: 14px;
      // text-indent: 1em;

      &::placeholder {
        font-size: 14px;
        color: rgb(188, 188, 188);
      }
    }

    textarea {
      width: 90%;
      height: 200px;
      outline: none;
      border: none;
      resize: none;
      margin-bottom: 20px;
      box-sizing: border-box;
      font-size: 14px;

      &::placeholder {
        font-size: 14px;
        color: rgb(188, 188, 188);
      }
    }

    /deep/ .el-upload {
      border: 1px dashed #ee8f23;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      margin-bottom: 20px;
    }

    /deep/ .el-upload:hover {
      border-color: #ee8f23;
    }
  }
}
</style>